<?php


namespace app\api\controller;
use think\facade\Db;

class Message
{
    private function getAdminId(){
        $token = request()->header('token');
        $checkToken = checkToken($token);
        $uid = $checkToken['data']->uid;
        return $uid;
    }
    //获取数据
    public function getMessage(){
        $data = request()->param();
        $page = (int)$data['page'];
        $limit = (int)$data['limit'];
        unset($data['page']);
        unset($data['limit']);
        $result2['submit_name'] = ['=',$data['submit_name']??''];
        $result2['submit_time'] =  ['between', [$data['date1'] ?? '',$data['date2'] ?? '']];
        $result2['admin_id'] = ['=',$this->getAdminId()];
        $w = array();
        foreach($result2 as $k=>$v) {
            if(is_array($v[1])){
                $start_time = $v[1][0];
                $end_time = $v[1][1];
                if($start_time!=""&&$end_time!=""){
                    $w[] = "$k $v[0] '$start_time' AND '$end_time'";
                }
            }else{
                if($v[1]!=""){
                    $w[] = "$k $v[0] '$v[1]'";
                }
            }

        }
        $where = join(' and ', $w);
        $submit_data = Db::table('messages')
            ->page($page,$limit)
            ->where($where)
            ->select()->toArray();
        return json(['status'=>200,'msg'=>'ok','data'=>$submit_data,'total'=>count($submit_data)]);
    }

    public function addMessage(){
        $data = request()->param();
        $data['admin_id'] =  $this->getAdminId();
        $res = Db::table('messages')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>500,'msg'=>'fail']);
        }
    }
    public function delMessage(){
        $data = request()->param();
        $res = Db::table('messages')
            ->where('messages_id',$data['messages_id'])
            ->where('admin_id',$this->getAdminId())
            ->delete();
        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>500,'msg'=>'fail']);
        }
    }
    public function delAllMessage(){
        $data = request()->param();
        $token = request()->header('token');
        $checkToken = checkToken($token);
        if($checkToken['status']==10001){
            $uid = $checkToken['data']->uid;
            $admin_password = Db::table('admin')->where('admin_id',$uid)->value('admin_password');
            $check_data = checkPassword($data['admin_password'],$admin_password);
            if($check_data['status']==500){
                return json(['status'=>500,'msg'=>'密码错误！']);
            }
            $all_del =  Db::table('messages')->where('admin_id',$uid)->delete(true);
            if($all_del){
                return json(['status'=>200,'msg'=>'全部删除成功！']);
            }else{
                return json(['status'=>500,'msg'=>'全部删除失败！']);
            }

        }else{
            return json(['status'=>501,'msg'=>$checkToken['data']]);
        }
    }
    public function updateMessage(){
        $data = request()->param();
        if(isset($data['admin_id'])){
            unset($data['admin_id']);
        }
        $res = Db::table('messages')
            ->where('messages_id',$data['messages_id'])
            ->where('admin_id',$this->getAdminId())
            ->update($data);
        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else {
            return json(['status' => 500, 'msg' => 'fail']);
        }
    }
    public function getMessageAdminAll(){
        $res = Db::table('messages')
            ->where('admin_id',$this->getAdminId())
            ->select();
        return json(['status'=>200,'data'=>$res]);
    }
}